package com.itheima.springmvc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.itheima.springmvc.pojo.Items;
import com.itheima.springmvc.pojo.QueryVo;
import com.itheima.springmvc.service.ItemService;

@Controller
public class ItemController {
	@Autowired
	private ItemService itemService;
	
	@RequestMapping(value = "/item/itemlist.action")
	public ModelAndView itemList() {
		//创建页面显示的商品数据
		List<Items>list = itemService.selectItemsList();
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("itemList",list);
		mav.setViewName("itemList");
		return mav;
	}
	
	//去修改页面的入参id
	@RequestMapping(value = "/itemEdit.action")
	public ModelAndView toEdit(Integer id, HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {
		
		//servle开发
		//String id = request.getParameter("id");
		
		//查询一个商品
		//Items items = itemService.selectItemsById(Integer.parseInt(id));
		Items items = itemService.selectItemsById(id);
		ModelAndView mav = new ModelAndView();
		mav.addObject("item", items);
		mav.setViewName("editItem");
		return mav;
	}
	
	@RequestMapping(value = "/updateitem.action")
	//public ModelAndView updateItems(Items items) {
	public ModelAndView updateItems(QueryVo vo) {
		itemService.updateItemsById(vo.getItems());
		ModelAndView mav = new ModelAndView();
		mav.addObject("item", vo.getItems());
		mav.setViewName("editItem");
		return mav;
	}
	
}
